<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

require_once ADMIN.DS.HEAD;
?>

<div class='full'>
	<div class="submenu_container">
		<a class="submenu_button" href='/admin/report/look'>Просмотр отчетов</a>
		<a class="submenu_button" href='/admin/report/create'>Создать отчет</a>
		<a class="submenu_button submenu_button_active" href='/admin/polls/view'>Просмотр опросов</a>
		<a class="submenu_button" href='/admin/polls/create'>Создать опрос</a>
		<a class="submenu_button" href='/admin/candidates/view'>Кандидаты</a>
	</div>
	<h3>Просмотр опросов</h3>
	<?php
	$a_list = $ua_list = array();
	$polls = $db->getTable("select * from `polls` order by `order` desc,`time` desc");
	$answers = $db->getTable("select `poll_id`,count(`answer_id`) as `count` from `answers` group by `poll_id`");
	$u_answers = $db->getTable("select `poll_id`,count(`ua_id`) as `count` from `user_answers` group by `poll_id`");
	
	foreach ($polls as $poll)
		$a_list[$poll['poll_id']]['count'] = $ua_list[$poll['poll_id']]['count'] = 0;
	foreach ($answers as $answer)
		$a_list[$answer['poll_id']] = $answer['count'];
	foreach ($u_answers as $ua)
		$ua_list[$ua['poll_id']] = $ua['count'];
	
	if ($polls)
	{
		echo "<table class='message_list'>";
		echo "<tr class='messages_header'><td width='50'>Приоритет</td><td width='100'>Дата</td><td>Текст вопроса</td><td>Статистика</td><td class='actions'>Действия</td></tr>";
		for ($i = 0; $i < count($polls); $i++)
		{
			echo "<tr>";
			echo "<td>".$polls[$i]['order']."</td>";
			echo "<td>".date("d.m.Y",$polls[$i]['time'])."</td>";
			echo "<td>".$polls[$i]['text']."</td>";
			echo "<td>";
				echo "<p>Вариантов ответа: ".$a_list[$polls[$i]['poll_id']]['count']."</p>";
				echo "<p>Всего ответило: ".$ua_list[$polls[$i]['poll_id']]['count']."</p>";
			echo "</td>";
			echo "<td class='actions'><a href='/admin/polls/edit/".$polls[$i]['poll_id']."' class='send'>Редактировать</a><br /><a href='/admin/polls/delete/".$polls[$i]['poll_id']."' class='delete'>Удалить</a></td>";
			echo "</tr>";
		}
		echo "</table>";
	}
	?>
</div>

<?php require_once ADMIN.DS.FOOTER; ?>